package org.bigdata.platform.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.bigdata.platform.exception.ErrorCode;
import org.bigdata.platform.exception.PlatformException;

public class UrlUtils {
	public static String getPageFromUrl(String url) throws PlatformException {
		
		StringBuilder page = new StringBuilder();
		
		try {
			URL _url = new URL(url);

			HttpURLConnection uc = (HttpURLConnection) _url.openConnection();
			uc.connect();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
			
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				page.append(inputLine);
			}
			
			in.close();

		} catch (MalformedURLException e) {
			throw new PlatformException(ErrorCode.INVALID_URL);
		} catch (IOException e) {
			throw new PlatformException(ErrorCode.UNKNOWN);
		}
		return page.toString();
	}
}
